package com.pdf.demo.util;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther:
 * @Date: 2019/12/05/16:34
 * @Description:
 */
public class ImageUtil {

    private static final String PATH = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    //柱形图路径
    private static final String IMAGE_PATH = PATH+"static/images/";

    public String getImage() throws Exception {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);
        JFreeChart chart = ChartFactory.createBarChart3D(
                "", // 图表标题
                "", // 目录轴的显示标签
                "单位:%", // 数值轴的显示标签
                dataset, // 数据集
                PlotOrientation.VERTICAL, // 图表方向：水平、垂直
                true,  // 是否显示图例(对于简单的柱状图必须是 false)
                false, // 是否生成工具
                false  // 是否生成 URL 链接
        );

        FileOutputStream fos_jpg = null;
        //String path = IMAGE_PATH+imageName+".jpg";
        String path = "d:/456.jpg";
        try {
            fos_jpg = new FileOutputStream(path);
            ChartUtilities.writeChartAsJPEG(fos_jpg,chart,400,300);
        } finally {
            try {
                fos_jpg.close();
            } catch (Exception e) {}
        }
        return path;
    }

    public static String categoryData(String imageName, Integer width, Integer height, List<String> datas, String title) throws Exception {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i = 0; i < datas.size(); i++) {
            String str = datas.get(i);
            String[] strs = str.split("\\|");
            dataset.addValue(new BigDecimal(strs[1]).doubleValue(), "", strs[0]);
        }
        /*dataset.addValue(120,"", "2020-10-13");
        dataset.addValue(125,"", "2020-10-14");
        dataset.addValue(123,"", "2020-10-15");*/
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,5));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);
        JFreeChart jFreeChart = ChartFactory.createLineChart(title, "", "", dataset,
                PlotOrientation.VERTICAL, true, true, false);
        jFreeChart.setTextAntiAlias(false);
        jFreeChart.removeLegend();
        CategoryPlot categoryPlot = jFreeChart.getCategoryPlot(); //获取绘图区对象
        categoryPlot.setBackgroundPaint(Color.WHITE); //设置背景颜色
        categoryPlot.setOutlinePaint(Color.WHITE); //设置边框颜色
        CategoryAxis domainAxis = categoryPlot.getDomainAxis();
        domainAxis.setLabelFont(new Font("隶书",Font.PLAIN,10)); //设置X轴标题文字字体
        domainAxis.setTickLabelFont(new Font("隶书",Font.PLAIN,10)); //设置X轴坐标值字体
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) categoryPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.BLACK);
        //显示数据点,设置形状
        renderer.setBaseShapesVisible(true);
        // 单系列设置
        renderer.setSeriesFillPaint(0, Color.BLACK);// 数据点中填充颜色
        renderer.setSeriesPaint(0, Color.BLACK);// 设置绘制线条的颜色,包括了外廓线的颜色
        renderer.setSeriesOutlinePaint(0, Color.BLUE, true);//无效果
        renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F), true);// 数据点外廓线粗细
        FileOutputStream fos_jpg = null;
        String path = IMAGE_PATH + imageName + ".jpg";
        //String path = imageName + ".jpg";
        try {
            fos_jpg = new FileOutputStream(path);
            ChartUtilities.writeChartAsJPEG(fos_jpg, jFreeChart,width,height);
        } finally {
            try {
                fos_jpg.close();
            } catch (Exception e) {}
        }
        return path;
    }

    public static void main(String[] args) {
        String[] str = "2019-05-15|61".split("\\|");
        System.out.println(str.length);
    }


}
